/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package Database;

import SteamEngineDB.Schedule;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.joda.time.LocalDate;
import org.junit.*;

/**
 *
 * @author Jeff Bendixsen <jeff@westcoastcode.com>
 */
public class DeleteAllSchedules {
    
    private EntityManager em = null;
    private EntityTransaction tx;
    
    public DeleteAllSchedules() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }
    
    @Before
    public void setUp() {
        em = Persistence.createEntityManagerFactory("SteamEngineDBPU").createEntityManager();
        
        assert(em != null);
        
        tx = em.getTransaction();
        
        assert(tx != null);
    }
    
    @After
    public void tearDown() {
    }
    @Test
    public void deleteAllSchedules() {
        
        System.out.println("Starting deleteAllSchedules");
        
        LocalDate today = new LocalDate(); 
          
        List<Schedule> schedules = em.createNamedQuery("Schedule.findAll")
                .getResultList();
        
        int deletedCount = 0;
        for(Iterator<Schedule> i = schedules.iterator(); i.hasNext();){
            Schedule r = i.next();
            
            System.out.printf("Removing schedule %s\n", r.getScheduleDate().toString());
            
            tx.begin();
            em.remove(r);
            tx.commit();
            
            
            deletedCount++;
        }
        System.out.printf("Deleted %d schedules\n", deletedCount);
           
    }
}
